package May._11.work2.Dao;

import untils.ConnectionUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class AccountDaoImpl implements AccountDao{
    @Override
    public BigDecimal selectMoneyByName(String name) {
        try {
            Connection connection = ConnectionUtils.getConnection();

            String sql="SELECT * FROM account WHERE name=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            ResultSet resultSet = preparedStatement.executeQuery();
            int money=0;
            if(resultSet.next()){
                money = resultSet.getInt("money");
                System.out.println(money);

            }
            // System.out.println(resultSet.getInt("money"));
            return new BigDecimal(money);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateMoneyByName(String name, BigDecimal money) {
        try {
            Connection connection = ConnectionUtils.getConnection();
            String sql="UPDATE account SET money=? WHERE name=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,money.intValue());
            preparedStatement.setString(2,name);
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 0;
    }
}
